#include "menutriggeraction.h"

MenuTriggerAction::MenuTriggerAction(QObject *parent)
    : MenuTriggerAction{"", parent}
{

}

MenuTriggerAction::MenuTriggerAction(const QString &text, QObject *parent)
    : MenuTriggerAction{QIcon(), text, parent}
{

}

MenuTriggerAction::MenuTriggerAction(const QIcon &icon, const QString &text, QObject *parent)
    : QAction{icon, text, parent}
    , m_tableWidget(nullptr)
    , m_callback([](auto item){})
    , m_isSimple(false)
    , m_simpleCallback([](){})
{
    connect(this, &QAction::triggered, this, [this](){
        if (this->m_isSimple) {
            this->m_simpleCallback();
        } else if (this->m_tableWidget->currentItem()) {
            this->m_callback(this->m_tableWidget->currentItem());
        }
    });
}

menu_trigger_action_callback MenuTriggerAction::callback() const
{
    return m_callback;
}

void MenuTriggerAction::setCallback(const menu_trigger_action_callback &newCallback)
{
    m_callback = newCallback;
}

QTableWidget *MenuTriggerAction::tableWidget() const
{
    return m_tableWidget;
}

void MenuTriggerAction::setTableWidget(QTableWidget *newTableWidget)
{
    m_tableWidget = newTableWidget;
}

void MenuTriggerAction::setTableWidgetCallback(QTableWidget *newTableWidget, const menu_trigger_action_callback &newCallback)
{
    setTableWidget(newTableWidget);
    setCallback(newCallback);
}

std::function<void ()> MenuTriggerAction::simpleCallback() const
{
    return m_simpleCallback;
}

void MenuTriggerAction::setSimpleCallback(const std::function<void ()> &newSimpleCallback)
{
    m_isSimple = true;
    m_simpleCallback = newSimpleCallback;
}
